package chen.web.back.client;

import java.util.List;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TabPanelEvent;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.TabItem;

import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class Workbench {

	private Viewport veiwport;
	private ContentPanel navigation;
	private TabPanel tabPanel;
	
	private Workbench() {
		veiwport = new Viewport();
		veiwport.setLayout(new BorderLayout());
		
		veiwport.add(new TopArea(), new BorderLayoutData(LayoutRegion.NORTH, 25.0f));
		
		navigation = new ContentPanel();
		navigation.setHideCollapseTool(true);
		navigation.setHeading("菜单");
		navigation.getHeader().setStyleAttribute("font-size", "14px");
		navigation.setCollapsible(true);
		AccordionLayout al_contentPanel = new AccordionLayout();
		navigation.setLayout(al_contentPanel);
		
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 160);  
		westData.setSplit(true);  
		westData.setCollapsible(true);  
		westData.setMargins(new Margins(0,5,0,0));  
		veiwport.add(navigation, westData);
		
		tabPanel = new TabPanel();
		tabPanel.setMinTabWidth(115);  
		tabPanel.setResizeTabs(true);  
		tabPanel.setAnimScroll(true);  
		tabPanel.setTabScroll(true);  
		tabPanel.setCloseContextMenu(true); 
		
		tabPanel.addListener(Events.Select, new Listener<TabPanelEvent>(){

			@Override
			public void handleEvent(TabPanelEvent be) {
				// TODO Auto-generated method stub
				TabItem tab = be.getItem();
				String anchor = tab.getData("anchor");
				if(anchor != null && !anchor.equals(History.getToken())){
					Window.Location.assign(Window.Location.createUrlBuilder().setHash(anchor).buildString());
					be.cancelBubble();
				}
			}
			
		});
		
		veiwport.add(tabPanel, new BorderLayoutData(LayoutRegion.CENTER));
	}
	
	public void addNavigationItem(Widget generator){
		navigation.add(generator);
	}
	
	public void addTabItem(TabItem item){
		List<TabItem> list = tabPanel.getItems();
		String anchor = item.getData("anchor");
		if(anchor != null){
			for(TabItem tab : list){ 
				if(anchor.equals(tab.getData("anchor"))){
					tabPanel.setSelection(tab);
					return;
				}
			}	
		}
		tabPanel.add(item);
		tabPanel.setSelection(item);
	}
	

	public TabItem generalTabItem(String title){
		TabItem tab = new TabItem(title);
		tab.setClosable(true);
		tab.setStyleAttribute("padding-top", "10px");
		tab.setStyleAttribute("padding-left", "10px");
		tab.getHeader().setStyleAttribute("font-size", "14px");
		
		//设置第一个item不能关闭
		List<TabItem> list = tabPanel.getItems();
		if(list == null || list.size() == 0){
			tab.setClosable(false);
		}
		return tab;
	}
	
	public TabItem generalTabItem(Hyperlink link){
		return generalTabItem(link.getText(), link.getTargetHistoryToken());
	}
	
	public TabItem generalTabItem(Entry entry){
		return generalTabItem(entry.getName(), entry.getURL());
	}
	
	public TabItem generalTabItem(String title, String anchor){
		TabItem tab = generalTabItem(title);
		tab.setData("anchor", anchor);
		return tab;
	}
	
	
	private static Workbench bench;
	public static Workbench getInstance(){
		if(bench == null){
			bench = new Workbench();
		}
		return bench;
	}
	
	public void show(){
		RootPanel.get().add(veiwport);
	}
	
	public void hide(){
		RootPanel.get().remove(veiwport);
	}

}
